#!/usr/bin/python
# -*- coding:utf-8 -*-

from models.user import User


def get_operator(request):
    """根据登录用户查询日志范围"""
    if request.user.is_superuser and request.user.username == "admin":
        return {}
    if request.user.is_superuser and request.user.username != "admin":
        operator = [u.username for u in User.objects.filter(parent_id=request.user.user_id)]
        operator.append(request.user.username)
        return {"operator__in": operator}
    if (not request.user.is_superuser) and request.user.parent_id:
        parent = User.objects.filter(user_id=request.user.parent_id).first()
        # admin 创建的审计员可以查看所有日志
        if parent.username == "admin":
            return {}
        operator = [u.username for u in User.objects.filter(parent_id=request.user.parent_id)]
        operator.append(parent.username)
        return {"operator__in": operator}
    return {}
